package com.shop.controller;


import com.shop.entity.MerchantBasket;
import com.shop.entity.MerchantGoods;
import com.shop.model.vo.Rsp;
import com.shop.service.MerchantBasketService;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * (MerchantBasket)表控制层
 *
 * @author makejava
 * @since 2020-11-24 10:20:19
 */
@Validated
@RestController
@AllArgsConstructor
@RequestMapping("basket")
public class MerchantBasketController {
    /**
     * 服务对象
     */
    private final MerchantBasketService merchantBasketService;

    @GetMapping("/getBasketList")
    public Rsp getBasketList(@RequestHeader("userId") Integer userId){
//        System.out.println(userId);
//        System.out.println(merchantBasketService.getBasketList(userId).getContent());
        return merchantBasketService.getBasketList(userId);
    }

    @PostMapping("/changeItem")
    public Rsp changeItem(@RequestHeader("userId") Integer userId, @RequestBody MerchantBasket merchantBasket) {
        return merchantBasketService.changeItem(userId, merchantBasket);
    }

    @PostMapping("addGoodsToCart")
    public Rsp addGoodsToCart(@RequestHeader("userId") Integer userId,@RequestBody MerchantGoods merchantGoods) {
       // System.out.println(userId);
        //System.out.println(merchantGoods.toString());
        return  new Rsp(200, "", merchantBasketService.addGoodsToCart(userId,merchantGoods));
    }

    @GetMapping("getCartListByUserId")
    public Rsp getCartListByUserId(@RequestHeader("userId") Integer userId,String merchantId)
    {
        return new Rsp(200,"",merchantBasketService.getCartListByUserId(userId,merchantId));
    }

    @PostMapping("updateGoodsIndex")
    public Rsp updateGoodsIndex(@RequestBody MerchantBasket merchantBasket){
        return  new Rsp(200, "", merchantBasketService.updateGoodsIndex(merchantBasket));
    }

    @GetMapping("/getBasketsCount")
    public Rsp getBasketsCount(@RequestHeader("userId") Integer userId) {
        return merchantBasketService.getBasketsCount(userId);
    }

    @GetMapping("/totalPay")
    public Rsp totalPay(@RequestHeader("userId") Integer userId, String shopCartIds) {
        String substring = shopCartIds.substring(1,shopCartIds.length()-1);
        String[] ids = substring.split(",");
        List<String> list = Arrays.asList(ids);
//        List<Character> list = Arrays.asList(ArrayUtils.toObject(shopCartIds.toCharArray()));
        return merchantBasketService.totalPay(userId,list);
    }

    @GetMapping("/deleteItem")
    public Rsp deleteItem(String basketIds){
        System.out.println(basketIds);
        String substring = basketIds.substring(1,basketIds.length()-1);
        String[] ids = substring.split(",");
        List<String> list = Arrays.asList(ids);
        System.out.println(list.toString());
        return merchantBasketService.deleteItem(list);
    }
}